<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="!promotionId">
      <el-col :span="1.5">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['sms:flashPromotionSession:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['sms:flashPromotionSession:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['sms:flashPromotionSession:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['sms:flashPromotionSession:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="flashPromotionSessionList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="场次名称" align="center" prop="name" />
      <el-table-column label="每日开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="每日结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === 0 ? '不启用' : '启用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!promotionId"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['sms:flashPromotionSession:edit']"
          >修改</el-button>
          <el-button v-if="!promotionId"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['sms:flashPromotionSession:remove']"
          >删除</el-button>
          <el-button v-if="promotionId"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="$router.push({ path:'/sms/flashPromotionProductRelation',
                     query:{ flashPromotionSessionId: scope.row.id, flashPromotionId: promotionId } })"
                     v-hasPermi="['sms:flashPromotionProductRelation:list']"
          >商品列表</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改限时购场次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="场次名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入场次名称" />
        </el-form-item>
        <el-form-item label="每日开始时间" prop="startTime">
          <el-time-picker clearable size="small" style="width: 200px"
                          v-model="form.startTime"
                          placeholder="选择每日开始时间"
                          value-format="HH:mm:ss">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="每日结束时间" prop="endTime">
          <el-time-picker clearable size="small" style="width: 200px"
                          v-model="form.endTime"
                          placeholder="选择每日结束时间"
                          value-format="HH:mm:ss">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">不启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlashPromotionSession, getFlashPromotionSession, delFlashPromotionSession, addFlashPromotionSession, updateFlashPromotionSession, exportFlashPromotionSession } from "@/api/sms/flashPromotionSession";

export default {
  name: "FlashPromotionSession",
  components: {
  },
  computed: {
    promotionId() {
      return this.$route.query.promotionId;
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 限时购场次表格数据
      flashPromotionSessionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "场次名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "每日开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "每日结束时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "启用状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询限时购场次列表 */
    getList() {
      this.loading = true;
      listFlashPromotionSession().then(response => {
        this.flashPromotionSessionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        startTime: null,
        endTime: null,
        status: 1,
        createTime: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加限时购场次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlashPromotionSession(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改限时购场次";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlashPromotionSession(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlashPromotionSession(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除限时购场次编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delFlashPromotionSession(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有限时购场次数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportFlashPromotionSession();
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>